package com.ghxxzx.dagl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ghxxzx.dagl.entity.Dalx;
import com.ghxxzx.dagl.entity.Daxx;
import com.ghxxzx.dagl.entity.DaxxWs;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.DalxMapper;
import com.ghxxzx.dagl.mapper.DaxxMapper;
import com.ghxxzx.dagl.mapper.DaxxWsMapper;
import com.ghxxzx.dagl.service.DaLxGlService;
import com.ghxxzx.dagl.utils.WsFlag;
import com.ghxxzx.dagl.vo.LxNode;
import com.ghxxzx.dagl.vo.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Node;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class DaLxGlServiceImpl implements DaLxGlService {
    @Autowired
    private DalxMapper dalxMapper;
    @Autowired
    private DaxxMapper daxxMapper;
    @Autowired
    private DaxxWsMapper daxxWsMapper;
    @Autowired
    private WsFlag wsFlag;
    @Override
    public ResultEntity deleteDalx(Integer id) {
        QueryWrapper<Dalx> dalxQueryWrapper = new QueryWrapper<>();
       dalxQueryWrapper.eq("parent_id", id);
        List<Dalx> selectList = dalxMapper.selectList(dalxQueryWrapper);
        if (selectList!=null&&selectList.size()>0){
            StringBuffer stringBuffer = new StringBuffer();
            for (int i = 0; i < selectList.size(); i++) {
                stringBuffer.append("["+selectList.get(i).getDalxName()+"]");
            }
            throw new JyException("该档案类型和"+stringBuffer.toString()+"已关联");
        }
        QueryWrapper<Daxx> daxxQueryWrapper = new QueryWrapper<>();
        daxxQueryWrapper.eq("type_id",id);
        List<Daxx> daxxes = daxxMapper.selectList(daxxQueryWrapper);
        if (daxxes==null){
            daxxes=new ArrayList<>();
        }
        QueryWrapper<DaxxWs> daxxWsQueryWrapper = new QueryWrapper<>();
        daxxWsQueryWrapper.eq("type_id",id);
        List<DaxxWs> daxxWs = daxxWsMapper.selectList(daxxWsQueryWrapper);
        if (daxxWs==null){
            daxxWs=new ArrayList<>();
        }
        if (daxxes.size()>0||daxxWs.size()>0){
            throw new JyException("已被档案录入引用,无法删除");
        }
        dalxMapper.deleteById(id);
        return ResultEntity.ok();
    }

    @Override
    public ResultEntity addDalx(String dalxName, Integer parentId,String code) {
        QueryWrapper<Dalx> wrapper = new QueryWrapper<>();
        wrapper.eq("dalx_name", dalxName);
        Dalx dalx = dalxMapper.selectOne(wrapper);
        if (dalx != null) {
            throw new JyException("该档案已存在");
        }
        dalx = new Dalx();
        dalx.setDalxName(dalxName);
        dalx.setParentId(parentId);
        dalx.setCode(code);
        dalxMapper.insert(dalx);
        return ResultEntity.ok();
    }


    private void generateOrganization(LxNode root, List<LxNode> organizations) {
        if(organizations == null || organizations.size() == 0){
            return;
        }
        List<LxNode> children = organizations.stream().filter(d -> d.getParentId() == Integer.parseInt(root.getId())).collect(Collectors.toList());
        root.setNodes(children);
        organizations.removeAll(children);
        for(LxNode o : children){
            generateOrganization(o,organizations);
        }
    }

    @Override
    public ResultEntity queryDalxNode() {
        List<Dalx> all = dalxMapper.finAll();
        List<LxNode> allLxNode = new ArrayList<>();
        for(Dalx dalx : all){
            allLxNode.add(getLxNode(dalx));
        }
        List<LxNode> roots = allLxNode.stream().filter(d -> d.getParentId() == 0).collect(Collectors.toList());
        all.remove(roots);
        for(LxNode root: roots){
            generateOrganization(root,allLxNode);
        }
        return ResultEntity.ok(roots);
    }

    private LxNode getLxNode(Dalx dalx) {
        LxNode lxNode = new LxNode();
        lxNode.setId(dalx.getId()+"");
        lxNode.setText(dalx.getDalxName());
        lxNode.setParentId(dalx.getParentId());
        return lxNode;
    }

//    @Override
//    public ResultEntity queryDalxNode() {
//        ArrayList<LxNode> lxNodes = new ArrayList<>();
//        List<Dalx> all = dalxMapper.finAll();
//        for (int i = 0; i < all.size(); i++) {
//            if (all.get(i).getParentId() == 0) {
//                Dalx dalx = all.get(i);
//                LxNode lxNode = getLxNode(dalx, dalx.getId());
//                lxNodes.add(lxNode);
//            }
//        }
//        for (int y = 0; y < lxNodes.size(); y++) {
//            LxNode lxNode = lxNodes.get(y);
//            ArrayList<LxNode> list = new ArrayList<>();
//            for (int i = 0; i < all.size(); i++) {
//                Dalx dalx = all.get(i);
//                if (dalx.getParentId() == lxNode.getId()) {
//                    LxNode lxNode1 = new LxNode();
//                    lxNode1.setText(dalx.getDalxName());
//                    lxNode1.setId(dalx.getId());
//                    list.add(lxNode1);
//                }
//            }
//            for (int i = 0; i < list.size(); i++) {
//                LxNode lxNode2 = list.get(i);
//                ArrayList<LxNode> arrayList = new ArrayList<>();
//                for (int s = 0; s < all.size(); s++) {
//                    Dalx dalx = all.get(s);
//                    if (dalx.getParentId() == lxNode2.getId()) {
//                        LxNode lxNode1 = new LxNode();
//                        lxNode1.setText(dalx.getDalxName());
//                        lxNode1.setId(dalx.getId());
//                        arrayList.add(lxNode1);
//                    }
//                }
//                lxNode2.setNodes(arrayList);
//            }
//            lxNode.setNodes(list);
//        }
//        return ResultEntity.ok(lxNodes);
//    }

    @Override
    public List<Dalx> findAll() {
        return dalxMapper.selectListNoRootandWs();
    }
}
